from rest_framework import serializers
from ..models.basicDimensionModel import *


class BasicDimensionListQuerySerializer(serializers.Serializer):
    basic_dimension_name = serializers.CharField(
        required=False, allow_blank=True)
    sei_id = serializers.CharField(required=False, allow_blank=True)
    basic_dimension_symbol = serializers.CharField(
        required=False, allow_blank=True)
    page_size = serializers.IntegerField(
        required=False, max_value=50, min_value=1)
    current_page = serializers.IntegerField(required=False)
    sort_prop = serializers.CharField(required=False, allow_blank=True)
    sort_order = serializers.CharField(required=False, allow_blank=True)

    def validate(self, attrs):
        filter_flag = True
        page_flag = True
        if "basic_dimension_name" in attrs:
            attrs["basic_dimension_name"] = attrs["basic_dimension_name"].strip()
            if attrs["basic_dimension_name"] == "":
                attrs.pop("basic_dimension_name")
        if "sei_id" in attrs:
            attrs["sei_id"] = attrs["sei_id"].strip()
            if attrs["sei_id"] == "":
                attrs.pop("sei_id")
        if "basic_dimension_symbol" in attrs:
            attrs["basic_dimension_symbol"] = attrs["basic_dimension_symbol"].strip()
            if attrs["basic_dimension_symbol"] == "":
                attrs.pop("basic_dimension_symbol")
        if ("basic_dimension_name" not in attrs or attrs["basic_dimension_name"] == "") and ("sei_id" not in attrs or attrs["sei_id"] == "") and ("basic_dimension_symbol" not in attrs or attrs["basic_dimension_symbol"] == ""):
            filter_flag = False
        if ("page_size" not in attrs):
            page_flag = False
        if not filter_flag and not page_flag:
            raise serializers.ValidationError({
                'detail': '查询条件过于宽泛'
            })
        try:
            attrs = super().validate(attrs)
        except serializers.ValidationError as e:
            raise serializers.ValidationError({
                'detail': '验证失败:' + str(e.detail)
            })
        return attrs

    @classmethod
    def get_query_fields(self):
        return [{"title": "基本量纲名称", "dataIndex": "basic_dimension_name", "type": "CharField"},
                {"title": "基本量纲编码：", "dataIndex": "sei_id", "type": "CharField"},
                {"title": "基本量纲符号", "dataIndex": "basic_dimension_symbol", "type": "CharField"}]


class BasicDimensionListSerializer(serializers.ModelSerializer):
    basic_dimension_name = serializers.SerializerMethodField()
    other_basic_dimension_symbol = serializers.SerializerMethodField()

    class Meta:
        model = BasicDimension
        fields = ['uuid', 'basic_dimension_name', 'sei_id', 'basic_dimension_symbol',
                  'other_basic_dimension_symbol', 'basic_dimension_description', 'basic_dimension_comment']

    def get_basic_dimension_name(self, obj):
        return obj.show_full_name()

    def get_other_basic_dimension_symbol(self, obj):
        symbol_dict = {}
        symbol_ascii = symbol_ascii = obj.get('basic_dimension_symbol_ascii') if isinstance(
            obj, dict) else obj.basic_dimension_symbol_ascii
        if symbol_ascii != "" and symbol_ascii is not None:
            symbol_dict["基本量纲ascii符号"] = symbol_ascii
        symbol_html = obj.get('basic_dimension_symbol_html') if isinstance(
            obj, dict) else obj.basic_dimension_symbol_html
        if symbol_html != "" and symbol_html is not None:
            symbol_dict["基本量纲html符号"] = symbol_html
        symbol_latex = obj.get('basic_dimension_symbol_latex') if isinstance(
            obj, dict) else obj.basic_dimension_symbol_latex
        if symbol_latex != "" and symbol_latex is not None:
            symbol_dict["基本量纲latex符号"] = symbol_latex
        symbol_unicode = obj.get('basic_dimension_symbol_unicode') if isinstance(
            obj, dict) else obj.basic_dimension_symbol_unicode
        if symbol_unicode != "" and symbol_unicode is not None:
            symbol_dict["基本量纲unicode符号"] = symbol_unicode
        symbol_dict_str = '\n'.join(
            f'{key}: {value}' for key, value in symbol_dict.items())
        return symbol_dict_str

    @classmethod
    def get_extra_fields_info(self):
        return [
            {"title": "基本量纲名称", "dataIndex": "basic_dimension_name", "type": "TextField"},
            {"title": "基本量纲其他符号信息", "dataIndex": "other_basic_dimension_symbol", "type": "TextField"}]


class BasicDimensionCreateSerializer(serializers.ModelSerializer):
    class Meta:
        model = BasicDimension
        fields = ["basic_dimension_name", "basic_dimension_name_en", "sei_id", "basic_dimension_symbol", "basic_dimension_symbol_ascii",
                  "basic_dimension_symbol_html", "basic_dimension_symbol_latex", "basic_dimension_symbol_unicode", "basic_dimension_description", "basic_dimension_description_en", "basic_dimension_comment"]

    def to_internal_value(self, data):
        data = super().to_internal_value(data)
        for field_name in self.fields:
            value = data.get(field_name)
            if isinstance(value, str):
                data[field_name] = value.strip()

        return data
        

class BasicDimensionModifyInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = BasicDimension
        fields = ["basic_dimension_name", "basic_dimension_name_en", "sei_id", "basic_dimension_symbol", "basic_dimension_symbol_ascii",
                  "basic_dimension_symbol_html", "basic_dimension_symbol_latex", "basic_dimension_symbol_unicode", "basic_dimension_description", "basic_dimension_description_en", "basic_dimension_comment"]
    

class BasicDimensionModifyInfoInSerializer(serializers.ModelSerializer):
    class Meta:
        model = BasicDimension
        fields = ["basic_dimension_name", "basic_dimension_name_en", "sei_id", "basic_dimension_symbol", "basic_dimension_symbol_ascii",
                  "basic_dimension_symbol_html", "basic_dimension_symbol_latex", "basic_dimension_symbol_unicode", "basic_dimension_description", "basic_dimension_description_en", "basic_dimension_comment"]

    def to_internal_value(self, data):
        data = super().to_internal_value(data)
        for field_name in self.fields:
            value = data.get(field_name)
            if isinstance(value, str):
                data[field_name] = value.strip()

        return data


class BasicDimensionInfoSerializer(serializers.ModelSerializer):
    create_person = serializers.SerializerMethodField()
    modify_person = serializers.SerializerMethodField()

    class Meta:
        model = BasicDimension
        fields = ["basic_dimension_name", "basic_dimension_name_en", "sei_id", "basic_dimension_symbol", "basic_dimension_symbol_ascii",
                  "basic_dimension_symbol_html", "basic_dimension_symbol_latex", "basic_dimension_symbol_unicode", "basic_dimension_description", "basic_dimension_description_en", "basic_dimension_comment", "create_person", "create_date", "modify_person", "modify_date"]

    def get_create_person(self, obj):
        return obj.create_person.user_name

    def get_modify_person(self, obj):
        return obj.modify_person.user_name
